package ufpb.compiladores.sintatico;

import java.util.LinkedList;

/**
 *
 * @author SemiNick
 */
public class Clause {

    private LinkedList<Entity> enty = new LinkedList<Entity>();
    private final Object lock = new Object();
    private final boolean opt;
    
    public Clause(boolean opt) {
        
        this.opt = opt;
    }
    
    public final void addEntity(Entity e) {
        
        if (e == null) {
            
            throw new NullPointerException("Entity cannot be null");
        }
        
        synchronized (this.lock) {
        
            this.enty.add(e);
        }
    }

    public final boolean isOptional() {
        
        return this.opt;
    }
    
    public final void removeEntity(Entity e) {

        if (e == null) {

            throw new NullPointerException("Entity cannot be null");
        }

        synchronized (this.lock) {

            this.enty.remove(e);
        }
    }

    public final Entity[] getEntities() {
        
        Entity[] ret = null;
        synchronized (this.lock) {
            
             ret = new Entity[enty.size()];
             enty.toArray(ret);
        }
        
        return ret;
    }
}
